package cn.daniellee.plugin.lm.listener;

import cn.daniellee.plugin.lm.core.LotteryCore;
import cn.daniellee.plugin.lm.core.PoolUsing;
import cn.daniellee.plugin.lm.menu.LotteryMenu;
import cn.daniellee.plugin.lm.menu.OverviewMenu;
import cn.daniellee.plugin.lm.model.PrizePool;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.Arrays;

public class MenuListener implements Listener {

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
        if (!(e.getWhoClicked() instanceof Player)) return;
        Player player = (Player) e.getWhoClicked();
        Inventory menu = e.getClickedInventory();
        if (menu != null && menu.getHolder() != null) {
            if (menu.getHolder() instanceof OverviewMenu.OverviewMenuHolder) {
                e.setCancelled(true);
                if (Arrays.asList(11, 13, 15, 20, 22, 24, 29, 31, 33).contains(e.getRawSlot())) {
                    ItemStack menuItem = e.getCurrentItem();
                    if (menuItem != null && menuItem.getItemMeta() != null) {
                        String id = menuItem.getItemMeta().getDisplayName();
                        id = id.substring(id.lastIndexOf("(") + 1, id.lastIndexOf(")"));
                        PrizePool prizePool = LotteryCore.getPrizePoolById(id);
                        if (e.isShiftClick()) {
                            if (e.isLeftClick()) {
                                // 编辑
                                player.openInventory(LotteryMenu.generate(prizePool, PoolUsing.UsingModel.EDITOR));
                            } else if (e.isRightClick()) {
                                // 开关


                            }
                        } else {
                            if (e.isLeftClick()) {
                                // 预览
                                player.openInventory(LotteryMenu.generate(prizePool, PoolUsing.UsingModel.LOTTERY));
                            } else if (e.isRightClick()) {
                                // 购买


                            }
                        }


                    }

                }
            } if (menu.getHolder() instanceof LotteryMenu.LotteryMenuHolder) {


            }
        }
    }
}